using System;
using System.Collections.Generic;
using System.Text;
using SlimDX;

namespace TileGert
{
  /// <summary>
  /// GertColor is a simple color with Red, Green, Blue and Alpha components in float format.
  /// The component values range from 0.0 to 1.0. Note that an alpha value of 1.0 means opaque.
  /// </summary>
  public class GertColor
  {
    private float _r = 0.0f;
    private float _g = 0.0f;
    private float _b = 0.0f;
    private float _a = 1.0f;

    /// <summary>
    /// Creates a new black, opaque GertColor.
    /// </summary>
    public GertColor()
    {
    }

    /// <summary>
    /// Creates a new opaque GertColor with the given color values.
    /// </summary>
    /// <param name="red">The red component.</param>
    /// <param name="green">The green component.</param>
    /// <param name="blue">The blue component.</param>
    public GertColor(float red, float green, float blue)
    {
      _r = red;
      _g = green;
      _b = blue;
    }

    /// <summary>
    /// Creates a new GertColor with the given color and alpha values.
    /// </summary>
    /// <param name="alpha">The alpha value.</param>
    /// <param name="red">The red component.</param>
    /// <param name="green">The green component.</param>
    /// <param name="blue">The blue component.</param>    
    public GertColor(float alpha, float red, float green, float blue)
    {
      _r = red;
      _g = green;
      _b = blue;
      _a = alpha;
    }

    /// <summary>
    /// Gets or sets the red component of the color. Default is 0.
    /// </summary>
    public float R
    {
      get { return _r; }
      set { _r = value; }
    }

    /// <summary>
    /// Gets or sets the green component of the color. Default is 0.
    /// </summary>
    public float G
    {
      get { return _g; }
      set { _g = value; }
    }

    /// <summary>
    /// Gets or sets the blue component of the color. Default is 0.
    /// </summary>
    public float B
    {
      get { return _b; }
      set { _b = value; }
    }

    /// <summary>
    /// Gets or sets the alpha value of the color. Default is 0.
    /// </summary>
    public float A
    {
      get { return _a; }
      set { _a = value; }
    }

    /// <summary>
    /// Convert the color to a SlimDX.Color4.
    /// </summary>
    /// <returns>The color as a SlimDX.Color4.</returns>
    internal Color4 ToColor4()
    {
      return new Color4(_a, _r, _g, _b);
    }

    /// <summary>
    /// Convert the color to a SlimDX.Color3.
    /// </summary>
    /// <returns>The color as a SlimDX.Color3.</returns>
    internal Color3 ToColor3()
    {
      return new Color3(_r, _g, _b);
    }
    
    /// <summary>
    /// Converts the color to a single int. Each component takes one byte of the four bytes.
    /// The order (starting with MSB) is ARGB.
    /// </summary>
    /// <returns>The color as an int.</returns>
    internal int ToInt() {
      int a = GertMisc.Clamp((int)(_a * 255), 0, 255) << 24;
      int r = GertMisc.Clamp((int)(_r * 255), 0, 255) << 16;
      int g = GertMisc.Clamp((int)(_g * 255), 0, 255) << 8;
      int b = GertMisc.Clamp((int)(_b * 255), 0, 255);
      
      /*a = a << 24;
      r = r << 16;
      g = g << 8;*/
      
      return a | r | g | b;
    }

    //TODO: Add more color templates
    private static GertColor _black = new GertColor(0, 0, 0);
    private static GertColor _white = new GertColor(1, 1, 1);
    private static GertColor _red = new GertColor(1, 0, 0);
    private static GertColor _green = new GertColor(0, 1, 0);
    private static GertColor _blue = new GertColor(0, 0, 1);
    private static GertColor _yellow = new GertColor(1, 1, 0);
    private static GertColor _navy = new GertColor(0, 0, 0.5f);

    /// <summary>
    /// The color black.
    /// </summary>
    public static GertColor Black
    {
      get { return _black; }
    }

    /// <summary>
    /// The color white.
    /// </summary>
    public static GertColor White
    {
      get { return _white; }
    }

    /// <summary>
    /// The color red.
    /// </summary>
    public static GertColor Red
    {
      get { return _red; }
    }

    /// <summary>
    /// The color green.
    /// </summary>
    public static GertColor Green
    {
      get { return _green; }
    }

    /// <summary>
    /// The color blue.
    /// </summary>
    public static GertColor Blue
    {
      get { return _blue; }
    }
    
    /// <summary>
    /// The color yellow.
    /// </summary>
    public static GertColor Yellow
    {
      get { return _yellow; }
    }

    /// <summary>
    /// The color navy.
    /// </summary>
    public static GertColor Navy
    {
      get { return _navy; }
      set { _navy = value; }
    }


  }
}
